package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CategoryBrandServiceImpl
 * Package: com.spzx.product.service.impl
 * Description:
 *
 * @Author ycy
 * @Create 2024/9/2 21:20
 * @Version 1.0
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询分类品牌列表
     *
     * @param categoryCategoryBrand 分类品牌
     * @return 分类品牌
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryCategoryBrand) {
        return categoryBrandMapper.selectCategoryBrandList(categoryCategoryBrand);
    }

    @Autowired
    private ICategoryService categoryService;

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = this.categoryBrandMapper.selectById(id);
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    @Override
    public List<Long> getAllCategoryIdList(Long id) {
        List<Long> list = new ArrayList<>();
        List<Category> categoryList = this.getParentCategory(id, new ArrayList<Category>());
        int size = categoryList.size();
        for (int i = size; i > 0; i--) {
            list.add(categoryList.get(i - 1).getId());
        }
        return list;
    }

    private List<Category> getParentCategory(Long id, List<Category> categoryList) {
        while (id > 0) {
            Category category = categoryMapper.selectOne(new LambdaQueryWrapper<Category>().eq(Category::getId, id).select(Category::getId, Category::getParentId));
            categoryList.add(category);
            return getParentCategory(category.getParentId(), categoryList);
        }
        return categoryList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if(count > 0) {
            throw new ServiceException("该分类已加该品牌");
        }
        return categoryBrandMapper.insert(categoryBrand);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand originalCategoryBrand = this.getById(categoryBrand.getId());
        if(categoryBrand.getCategoryId().longValue() != originalCategoryBrand.getCategoryId().longValue()
                || categoryBrand.getBrandId().longValue() != originalCategoryBrand.getBrandId().longValue()) {
            long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()).eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
            if(count > 0) {
                throw new ServiceException("该分类已加该品牌");
            }
        }
        return categoryBrandMapper.updateById(categoryBrand);
    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
    }

}
